﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameJamKingdom
{
    class MenuState : State
    {
        //SpriteFont font;
        Texture2D menuImage;
        Texture2D playButton;
        Texture2D playButtonSel;
        Texture2D creditsButton;
        Texture2D creditsButtonSel;

        Vector2 playPos;
        Vector2 creditsPos;

        int cButton;
        const int MAX_BUTTONS = 2;

        int timeElapsed;

        public MenuState(Game1 cGame)
        {
            Initialize(cGame);
        }

        public override void Initialize(Game1 cGame)
        {
            //font = cGame.Content.Load<SpriteFont>("ScoreFont");
            menuImage = cGame.Content.Load<Texture2D>("SnakeRunnerMainMenuBG");
            playButton = cGame.Content.Load<Texture2D>("PLAYbutton");
            playButtonSel = cGame.Content.Load<Texture2D>("PLAYbuttonSelected");
            creditsButton = cGame.Content.Load<Texture2D>("CREDITSbutton");
            creditsButtonSel = cGame.Content.Load<Texture2D>("CREDITSbuttonSelected");
            cButton = 0;

            playPos = new Vector2(Program.wWidth / 2 - playButton.Width / 2, 400);
            creditsPos = new Vector2(Program.wWidth / 2 - creditsButton.Width / 2, 500);
        }

        public override void Update(Game1 cGame, GameTime gameTime)
        {
            KeyboardState cKeyState;
            cKeyState = Keyboard.GetState();
            timeElapsed += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if ((cKeyState.IsKeyDown(Keys.Enter) || (cKeyState.IsKeyDown(Keys.Left))) && (timeElapsed > 100))
            {
                timeElapsed = 0;
                switch (cButton)
                {
                    case 0:
                        cGame.cState = "tutorial";
                        break;
                    case 1:
                        cGame.cState = "credits";
                        break;
                }
            }
            else if (cKeyState.IsKeyDown(Keys.Up))
            {
                if (cButton != 0)
                {
                    cButton -= 1;
                }
            }
            else if (cKeyState.IsKeyDown(Keys.Down))
            {
                if (cButton != MAX_BUTTONS - 1)
                {
                    cButton += 1;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            //string message2 = "Press 'Enter' to start";
            spriteBatch.Begin();
            spriteBatch.Draw(menuImage, new Rectangle(0, 0, Program.wWidth, Program.wHeight), Color.White);
            drawButtons(spriteBatch);
            spriteBatch.End();
        }

        private void drawButtons(SpriteBatch spriteBatch)
        {
            if (cButton == 0)
            {
                spriteBatch.Draw(playButtonSel, playPos, Color.White);
                spriteBatch.Draw(creditsButton, creditsPos, Color.White);
            }
            else if (cButton == 1)
            {
                spriteBatch.Draw(playButton, playPos, Color.White);
                spriteBatch.Draw(creditsButtonSel, creditsPos, Color.White);
            }
        }
    }
}
